package com.example.administrator.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.administrator.myapplication.uilts.PeiZhiActivity;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 董豪华 on 2017/1/6.
 */
public class DayAcitivity extends AppCompatActivity {
    private TextView name;
    private TextView datetime;
    private TextView summary;
    private TextView all;
    private TextView color;
    private TextView healthjin;
    private TextView lovejin;
    private TextView moneyjin;
    private TextView numberjin;
    private TextView QFriendjin;
    private TextView workjin;
    String xingzuo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dayxingzuo_item);
        Intent intent = getIntent();
        xingzuo = intent.getStringExtra("xingzuo");
        innitdata();
        shujufanhui(xingzuo);
    }


    private void innitdata() {
        name = (TextView) findViewById(R.id.namejin);
        datetime = (TextView) findViewById(R.id.datetimejin);
        summary = (TextView) findViewById(R.id.summaryjin);
        all = (TextView) findViewById(R.id.alljin);
        color = (TextView) findViewById(R.id.colorjin);
        healthjin = (TextView) findViewById(R.id.healthjin);
        lovejin = (TextView) findViewById(R.id.lovejin);
        moneyjin = (TextView) findViewById(R.id.moneyjin);
        numberjin = (TextView) findViewById(R.id.numberjin);
        QFriendjin = (TextView) findViewById(R.id.QFriendjin);
        workjin = (TextView) findViewById(R.id.workjin);
    }

    public void shujufanhui(String consNam){
        String url = "http://web.juhe.cn:8080/constellation/getAll?consName="+consNam+"&type=today&key=4ab3d0b2232c72d9c3dc4747baf8cf4f";
        //http://web.juhe.cn:8080/constellation/getAll?consName=狮子座&type=today&key=4ab3d0b2232c72d9c3dc4747baf8cf4f
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                        jiexijinri(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(DayAcitivity.this, (CharSequence) error,Toast.LENGTH_LONG).show();
            }
        });
        PeiZhiActivity.mrequestQueue.add(request);
    }
    /*今日或明日*/
    public void jiexijinri(String response){
        try {
            JSONObject jsonObject = new JSONObject(response);
            String name1 = jsonObject.optString("name");/*星座名称*/
            String datetime1 = jsonObject.optString("datetime");/*日期*/
            String date = jsonObject.optString("date");
            String all1 = jsonObject.optString("all");/*综合指数*/
            String color1 = jsonObject.optString("color");/*幸运色*/
            String health = jsonObject.optString("health");/*健康指数*/
            String love = jsonObject.optString("love");/*爱情指数*/
            String money = jsonObject.optString("money");/*财运指数*/
            String number = jsonObject.optString("number");/*幸运数字*/
            String qFriend = jsonObject.optString("QFriend");/*速配星座*/
            String summary1 = jsonObject.optString("summary");/*今日概述*/
            String work = jsonObject.optString("work");/*工作指数*/
            String error_code = jsonObject.optString("error_code");/*返回码*/
            name.setText(name1);
            datetime.setText(datetime1);
            summary.setText(summary1);
            all.setText(all1);
            color.setText(color1);
            healthjin.setText(health);
            lovejin.setText(love);
            moneyjin.setText(money);
            numberjin.setText(number);
            QFriendjin.setText(qFriend);
            workjin.setText(work);
        } catch (JSONException e) {
            e.printStackTrace();
            Toast.makeText(DayAcitivity.this,"解析异常",Toast.LENGTH_LONG).show();
        }
    }



}
